package cn.supermaster.management.web.service.system.impl;

import cn.supermaster.management.common.constant.UserConstant;
import cn.supermaster.management.common.domain.SystemDictionaryData;
import cn.supermaster.management.common.domain.SystemDictionaryType;
import cn.supermaster.management.common.entity.Ztree;
import cn.supermaster.management.common.page.TableDataInfo;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.common.utils.ShiroUtils;
import cn.supermaster.management.common.utils.WebHelper;
import cn.supermaster.management.common.utils.poi.ExcelUtil;
import cn.supermaster.management.system.service.ISysDictDataService;
import cn.supermaster.management.system.service.ISysDictTypeService;
import cn.supermaster.management.web.service.system.ISDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.List;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.service.system.impl
 * @ClassName: SDictionaryServiceImpl
 * @Description: [字典服务]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/2/20 11:08
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class SDictionaryServiceImpl implements ISDictionaryService {
    private String type_prefix = "system/dict/type";
    private String data_prefix = "system/dict/data";

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ISysDictDataService dictDataService;

    /*
     * ================================================
     *  页面跳转功能实现
     * ================================================
     */

    /**
     * @Description: [dictionary_type-跳转到字典类型界面]
     * <p></P>
     * @Title: skipDictionaryTypeIndexHtml
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Return: java.lang.String
     */
    @Override
    public String skipDictionaryTypeIndexHtml() {
        return type_prefix + "/type";
    }

    /**
     * @Description: [dictionary_type-跳转到字典类型Add界面]
     * <p></P>
     * @Title: skipDictionaryTypeAddHtml
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param:
     * @Return: java.lang.String
     * @Throws:
     */
    @Override
    public String skipDictionaryTypeAddHtml() {
        return type_prefix + "/add";
    }

    /**
     * @Description: [dictionary_type-跳转到字典类型Modify界面]
     * <p></P>
     * @Title: skipDictionaryTypeModifyHtml
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: modelMap
     * @Param: dictId
     * @Return: java.lang.String
     */
    @Override
    public String skipDictionaryTypeModifyHtml(ModelMap modelMap, Long dictId) {
        modelMap.put("dict", dictTypeService.selectDictTypeById(dictId));
        return type_prefix + "/edit";
    }

    /**
     * @Description: [dictionary_type-跳转到字典类型Detail界面]
     * <p></P>
     * @Title: skipDictionaryTypeDetailHtml
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: modelMap
     * @Param: dictId
     * @Return: java.lang.String
     */
    @Override
    public String skipDictionaryTypeDetailHtml(ModelMap modelMap, Long dictId) {
        modelMap.put("dict", dictTypeService.selectDictTypeById(dictId));
        modelMap.put("dictList", dictTypeService.selectDictTypeAll());
        return "system/dict/data/data";
    }

    /**
     * @Description: [dictionary_type-跳转到字典类型结构Tree界面]
     * <p></P>
     * @Title: skipDictionaryTreeHtml
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: modelMap
     * @Param: columnId
     * @Param: dictType
     * @Return: java.lang.String
     */
    @Override
    public String skipDictionaryTreeHtml(ModelMap modelMap, Long columnId, String dictType) {
        modelMap.put("columnId", columnId);
        modelMap.put("dict", dictTypeService.selectDictTypeByType(dictType));
        return type_prefix + "/tree";
    }

    /**
     * @Description: [dictionary_data-跳转到字典类型View界面]
     * <p></P>
     * @Title: skipDictionaryDataIndexHtml
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Return: java.lang.String
     */
    @Override
    public String skipDictionaryDataIndexHtml() {
        return data_prefix + "/data";
    }

    /**
     * @Description: [dictionary_data-跳转到字典类型Add界面]
     * <p></P>
     * @Title: skipDictionaryDataAddHtml
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: modelMap
     * @Param: dictType
     * @Return: java.lang.String
     */
    @Override
    public String skipDictionaryDataAddHtml(ModelMap modelMap, String dictType) {
        modelMap.put("dictType", dictType);
        return data_prefix + "/add";
    }

    /**
     * @Description: [dictionary_data-跳转到字典类型Modify界面]
     * <p></P>
     * @Title: skipDictionaryDataModifyHtml
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: modelMap
     * @Param: dictCode
     * @Return: java.lang.String
     */
    @Override
    public String skipDictionaryDataModifyHtml(ModelMap modelMap, Long dictCode) {
        modelMap.put("dict", dictDataService.selectDictDataById(dictCode));
        return data_prefix + "/edit";
    }


    /*
     * ================================================
     *  Rest功能实现
     * ================================================
     */

    /**
     * @Description: [dictionary_type-根据条件获取字典类型列表]
     * <p></P>
     * @Title: queryDictionaryTypeList
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: dictType
     * @Return: cn.supermaster.management.common.page.TableDataInfo
     */
    @Override
    public TableDataInfo queryDictionaryTypeList(SystemDictionaryType dictType) {
        WebHelper.startPage();
        List<SystemDictionaryType> list = dictTypeService.selectDictTypeList(dictType);
        return WebHelper.getDataTable(list);
    }

    /**
     * @Description: [dictionary_type-到处字典类型列表]
     * <p></P>
     * @Title: doExportDictionaryTypes
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: dictType
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doExportDictionaryTypes(SystemDictionaryType dictType) {
        List<SystemDictionaryType> list = dictTypeService.selectDictTypeList(dictType);
        ExcelUtil<SystemDictionaryType> util = new ExcelUtil<SystemDictionaryType>(SystemDictionaryType.class);
        return util.exportExcel(list, "字典类型");
    }

    /**
     * @Description: [dictionary_type-新增字典类型记录]
     * <p></P>
     * @Title: addDictionaryType
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: dictType
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse addDictionaryType(SystemDictionaryType dictType) {

        if (UserConstant.DICT_TYPE_NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dictType))) {
            return WebResponse.failure("新增字典'" + dictType.getDictName() + "'失败，字典类型已存在");
        }
        dictType.setCreateBy(ShiroUtils.getuserName());
        return WebHelper.webResult(dictTypeService.insertDictType(dictType));
    }

    /**
     * @Description: [dictionary_type-修改字典类型记录]
     * <p></P>
     * @Title: modifyDictionaryType
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: dict
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse modifyDictionaryType(SystemDictionaryType dict) {
        if (UserConstant.DICT_TYPE_NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return WebResponse.failure("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(ShiroUtils.getuserName());
        return WebHelper.webResult(dictTypeService.updateDictType(dict));
    }

    /**
     * @Description: [dictionary_type-删除字典类型记录]
     * <p></P>
     * @Title: deleteDictionaryType
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: ids
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse deleteDictionaryType(String ids) {
        return WebHelper.webResult(dictTypeService.deleteDictTypeByIds(ids));
    }

    /**
     * @Description: [dictionary_type-清除字典数据缓存]
     * <p></P>
     * @Title: cleanDictionaryCache
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse cleanDictionaryCache() {
        dictTypeService.clearCache();
        return WebResponse.success();
    }

    /**
     * @Description: [dictionary_type-校验字典类型唯一性]
     * <p></P>
     * @Title: doCheckDictionaryTypeUnique
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: dictType
     * @Return: java.lang.String
     */
    @Override
    public String doCheckDictionaryTypeUnique(SystemDictionaryType dictType) {
        return dictTypeService.checkDictTypeUnique(dictType);
    }

    /**
     * @Description: [dictionary_type-加载字典列表树]
     * <p></P>
     * @Title: loadDictionaryTree
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Return: java.util.List<cn.supermaster.management.common.entity.Ztree>
     */
    @Override
    public List<Ztree> loadDictionaryTree() {
        List<Ztree> ztrees = dictTypeService.selectDictTree(new SystemDictionaryType());
        return ztrees;
    }


    /////////////////////////////////////////////////////////

    /**
     * @Description: [dictionary_data-获取字典数据列表]
     * <p></P>
     * @Title: queryDictionaryDataList
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: dictData
     * @Return: cn.supermaster.management.common.page.TableDataInfo
     */
    @Override
    public TableDataInfo queryDictionaryDataList(SystemDictionaryData dictData) {
        WebHelper.startPage();
        List<SystemDictionaryData> list = dictDataService.selectDictDataList(dictData);
        return WebHelper.getDataTable(list);
    }

    /**
     * @Description: [dictionary_data-导出字典数据列表]
     * <p></P>
     * @Title: doExportDictionaryDatas
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: dictData
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doExportDictionaryDatas(SystemDictionaryData dictData) {
        List<SystemDictionaryData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SystemDictionaryData> util = new ExcelUtil<SystemDictionaryData>(SystemDictionaryData.class);
        return util.exportExcel(list, "字典数据");
    }

    /**
     * @Description: [dictionary_data-新增字典数据记录]
     * <p></P>
     * @Title: addDictionaryData
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: dictData
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse addDictionaryData(SystemDictionaryData dictData) {
        dictData.setCreateBy(ShiroUtils.getuserName());
        return WebHelper.webResult(dictDataService.insertDictData(dictData));
    }

    /**
     * @Description: [dictionary_data-修改字典数据记录]
     * <p></P>
     * @Title: modifyDictionaryData
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: dictData
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse modifyDictionaryData(SystemDictionaryData dictData) {
        dictData.setUpdateBy(ShiroUtils.getuserName());
        return WebHelper.webResult(dictDataService.updateDictData(dictData));
    }

    /**
     * @Description: [dictionary_data-删除字典数据记录]
     * <p></P>
     * @Title: deleteDictionaryDatas
     * @Author: Lance Ting
     * @Date: 2021-02-20
     * @Param: ids
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse deleteDictionaryDatas(String ids) {
        return WebHelper.webResult(dictDataService.deleteDictDataByIds(ids));
    }
}